﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/12/16 星期日 21:12:48
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 公司信息数据访问对象
    /// </summary>
    public class CompanyDAO : DAOBase<S7_Company>, ICompanyDAO
    {
        #region Public functions
        public override object Create(S7_Company entity)
        {
            string sql = @"
                INSERT INTO S7_Company
                        (UniqueId
                        ,Name
                        ,Introduce
                        ,Address
                        ,Postcode
                        ,Telephone
                        ,Logo
                        ,Map)
                    VALUES
                        (@UniqueId
                        ,@Name
                        ,@Introduce
                        ,@Address
                        ,@Postcode
                        ,@Telephone
                        ,@Logo
                        ,@Map)";

            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(entity.UniqueId, "UniqueId"),
                    DBUtil.CreateSqlParameter(entity.Name, "Name"),
                    DBUtil.CreateSqlParameter(entity.Introduce, "Introduce"),
                    DBUtil.CreateSqlParameter(entity.Address, "Address"),
                    DBUtil.CreateSqlParameter(entity.Postcode, "Postcode"),
                    DBUtil.CreateSqlParameter(entity.Telephone, "Telephone"),
                    DBUtil.CreateSqlParameter(entity.Logo, "Logo"),
                    DBUtil.CreateSqlParameter(entity.Map, "Map"));
            }
        }

        public override S7_Company Read(object primaryKey)
        {
            string sql = @"
                SELECT *
                    FROM S7_Company
                    WHERE UniqueId = @UniqueId";

            using (DBHelper opt = new DBHelper())
            {
                SqlDataReader reader = opt.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(primaryKey, "UniqueId"));

                object company = null;

                if (reader.Read())
                {
                    company = new S7_Company();

                    DBUtil.ReadData(reader, ref company);
                }

                return company as S7_Company;
            }
        }        
        #endregion
    }
}
